package com.plian.system.controller.transfer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.system.config.AppKeySign;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.im.ProjectInfo;
import com.plian.system.entity.sys.Org;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.service.fr.bank.IBankKeyService;
import com.plian.system.service.im.IProjectInfoService;
import com.plian.system.service.sys.orgzation.IOrgService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 项目基本信息 - 输出
 * @Date: 2022/11/8 4:34 PM
 * @Version: 1.0
 **/
@RestController
@RequestMapping("api/projectInfoTransfer")
@AllArgsConstructor
@Api(value = "项目基本信息-输出", tags = "项目基本信息输出")
@Slf4j
public class ProjectInfoTransferController {

    private IBankKeyService bankKeyService;

    private IProjectInfoService projectInfoService;

    private IOrgService orgService;

    /**
     * 提供时间段内的项目基本信息
     * @param dataPlatSign
     */
    @PostMapping(value = "/project-time")
    @PassToken
    @AppKeySign
    public R<List<ProjectInfo>> getProjectByTime(@RequestBody DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());
        List<ProjectInfo> list = projectInfoService.list(new LambdaQueryWrapper<ProjectInfo>().in(ProjectInfo::getCreateCompanyId, orgIds));
        if (Optional.ofNullable(dataPlatSign.getEndTime()).isPresent()){
            list = list.stream().filter(project -> Optional.ofNullable(project.getUpdateTime()).isPresent() &&
                    (dataPlatSign.getStartTime().compareTo(project.getUpdateTime()) < 1 && dataPlatSign.getEndTime().compareTo(project.getUpdateTime()) > -1)).collect(Collectors.toList());
        }
        return R.data(list);
    }
}
